package com.yr.ryjk.consult.dao;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.ResultType;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.yr.ryjk.consult.entity.Consult;
import com.yr.ryjk.consult.vo.ConsultVO;

public interface ConsultMapper {
	
	int insert(Consult consult);
	
	int insertSelective(Consult consult);
	
	Consult selectByPrimaryKey(String id);
	
	int deleteByPrimaryKey(String id);
	
	int updateByPrimaryKeySelective(Consult Consult);
	
	int updateByPrimaryKey(Consult Consult);
	
    List<Consult> selectConsultByDocId(int pageNo, int pageSize, String docId);
	
    @Select("select count(1) from consult where user_id=#{1} and status=#{0}")
    @ResultType(Integer.class)
	int selectCount(int status, String id);
    
    @Select("select count(1) from consult where doc_id=#{1} and status=#{0}")
    @ResultType(Integer.class)
    int selectCountOfDoctor(int status, String id);

    @Select("select * from consult")
    @ResultMap("BaseResultMap")
	List<Consult> selectAllConsults();

    @Select("select c.*,u.name as userName,d.real_name as docName " +
    		"from consult c " +
    			"left join user u on c.user_id=u.id " +
    			"left join doctor d on c.doc_id=d.id " +
    		"where c.id=#{0}")
    @ResultMap("VoResultMap")
    ConsultVO selectVoById(String id);

    @Update("update consult set status=#{status} where id=#{id}")
	void updateStatusById(Consult consult);

	List<ConsultVO> selectVoListOfUser(int i, int pageSize, int status, String id);

	List<ConsultVO> selectVoListOfDoctor(int i, int pageSize, int status,
			String id);

	@Select("select c.*,u.name as userName,d.real_name as docName " +
			"from consult c " +
			"left join user u on c.user_id=u.id " +
			"left join doctor d on c.doc_id=d.id " +
			"where c.doc_id=#{0} and c.user_id=#{1} " +
			"order by c.add_time desc "+
			"limit #{2},#{3}")
	@ResultMap("VoResultMap")
	List<ConsultVO> selectHistoryOfDoctorAndUser(String docId, String userId,
			int start, int pageSize);

	@Select("select count(1) from consult where doc_id=#{0} and user_id=#{1}")
	@ResultType(Integer.class)
	int selectCountHistory(String docId, String userId);
	
	@Select("select c.*,u.name as userName,d.real_name as docName,d.half_pic_url as docPicUrl " +
			"from consult c " +
				"left join user u on c.user_id=u.id " +
				"left join doctor d on c.doc_id=d.id " +
			"where u.phone=#{2} " +
				"and c.status=2 " +
				"and not exists(select * from score s where s.consult_id=c.id and s.user_id=u.id) " +
			"order by c.add_time desc " +
			"limit #{0},#{1}")
	@ResultMap("VoResultMap")
	List<ConsultVO> selectUnScoreList(int i, int pageSize, String phone);

	@Select("select count(1) " +
			"from consult c " +
				"left join user u on c.user_id=u.id " +
			"where u.phone=#{2} " +
				"and c.status=2 " +
				"and not exists(select * from score s where s.consult_id=c.id and s.user_id=u.id)")
	@ResultType(Integer.class)
	int selectUnScoreCount(String phone);
	
	@Delete("DELETE from consult where id=#{0}")
	void deleteConsultById(String id);

	@Select("select count(1) " +
			"from consult c " +
				"left join user u on c.user_id=u.id "+
			"where u.phone=#{0} and c.status=1")
	@ResultType(Integer.class)
	int selectUnHandledCountOfUser(String phone); 
}